package com.nmc.core.manager;

import java.util.Collection;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

/**
 * @author uday.kumar
 *
 */
public class EmailSenderImpl implements EmailSender{

	private JavaMailSender mailSender;
	public void sendBulkTexEmail(final Collection<String> to, final String from, final String subject, final String emailText) throws Exception {
	

		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

				// Get the array to addresses.
				Object[] toAddresses = to.toArray();
				int noOfRecipients = toAddresses.length;
				String[] addressTo = new String[noOfRecipients];
				for (int i = 0; i < noOfRecipients; i++) {
					addressTo[i] = (String) toAddresses[i];
				}

				//Set message To address list.
				message.setTo(addressTo);
				//Set email from address.
				message.setFrom(from);
				//Set email subject
				message.setSubject(subject);

				message.setText(emailText, true);
			}
		};
		
			//Send the mail.
			mailSender.send(preparator);
	
	
	}
	
	
	/**
	 * @param mailSender the mailSender to set
	 */
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}
	/**
	 * @return the mailSender
	 */
	public JavaMailSender getMailSender() {
		return mailSender;
	}

}
